package com.seeyon.apps.kk.form.workflowevent;

import java.io.File;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.v3x.mail.manager.MessageMailManager;
/**
 * 暂时无用作废
 * 流程撤销时 发送撤销通知邮件
 * 发送人从单据预设字段中获取
 * @author 
 *
 */
public class KksendEmailOnCancelExtendPlug extends KkBaseExtendPlug {
	private static final Log log = LogFactory.getLog(KksendEmailOnCancelExtendPlug.class);
	 
	private MessageMailManager messageMailManager; 
	
	
	public MessageMailManager getMessageMailManager() {
		return messageMailManager;
	}
	public void setMessageMailManager(MessageMailManager messageMailManager) {
		this.messageMailManager = messageMailManager;
	}
	@Override
	public String getId() {
		return "KksendEmailOnCancelExtendPlug";
	}
	@Override
	public String getLabel() {
		return "撤销时发送邮件";
	}
	@Override
	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}
	/**
	 * 提交前
	 */
	public WorkflowEventResult onBeforeFinishWorkitem(WorkflowEventData data){
	 
		
		return null;
	}
	/**
	 * 终止前
	 */
	public WorkflowEventResult onBeforeStop(WorkflowEventData data) {
		 
		return null;
	}
	/**
	 * 发起前
	 */
	@Override
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		 
		return null;
	}
	/**
	 * 发起者撤销前
	 */
	@Override
	public WorkflowEventResult onBeforeCancel(WorkflowEventData data) {
		 
		return null;
	}

	/**
	 * 撤销事件
	 */	
	
	public void onCancel(com.seeyon.ctp.workflow.event.WorkflowEventData data) {
		Map<String,Object> formMap=data.getBusinessData();	
		FormDataMasterBean fdmb = (FormDataMasterBean) formMap.get("formDataBean");
		FormTableBean ftb = fdmb.getFormTable();
		String tablekey=ftb.getTableName();
		String path="email"+File.separator+tablekey+"_onCancel.ftl";
		String jsonMail=this.freemarkFlowManager.fltTemplateIntoString(path, data);
		if(StringUtils.isNotBlank(jsonMail)) {
			JSONObject message=JSONObject.parseObject(jsonMail);
			String toEmail=message.getString("toEmail");
			String subject=message.getString("subject");
			String mailContent=message.getString("mailContent");
			String [] emails=StringUtils.split(toEmail, ",");
			for (int i = 0; i < emails.length; i++) {
				if(StringUtils.isNotBlank(emails[i])){
				messageMailManager.sendMessageByMail(emails[i], subject, mailContent.toString());
				}
			}
			
		}
		
		
		
	}
	
}
